#! /usr/bin/python

import os,sys, socket, popen2

def httpServer():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #s.bind((socket.gethostname(),8000))
    s.bind(("192.168.20.246",8000))
    s.listen(1)
    clientsocket,clientaddress = s.accept();
    clientsocket.send("welcome %s\n"%str(clientaddress))
    clientsocket.close()
    s.close()

#httpServer()
def echoclient(s):
    try:
        data = s.recv(1024)
        #while data:
        sys.stdout.write("received "+data)
        #data = s.recv(1024)
        s.send(data)
    except:
        sys.exit(0)
    else:
        s.close()

def forkServer():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind((socket.gethostname(), 8080))
        s.listen(10)
        while 1:
            clientsocket, clientaddress = s.accept();
            pid = os.fork()
            if pid == 0:
                s.close()
                echoclient(clientsocket)
            else:
                clientsocket.close()
    except:
        sys.exit(0)

forkServer()

#using select
import select

